﻿﻿<!DOCTYPE HTML>
<html>
<head>
    <title>BI Apps - Edit Report Variables</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="Stylesheet" type="text/css" href="../theme/css/style_reset.css" />
    <link rel="Stylesheet" type="text/css" href="../theme/css/style_admin_tabs.css" />
    <link rel="Stylesheet" type="text/css" href="../theme/css/button_content.css" />
    <link rel="Stylesheet" type="text/css" href="../theme/css/form_content.css" />
    <script type="text/JavaScript" src="../api/js/jquery-1.8.3.js"></script>
    <script type='text/JavaScript' src='../../zbw_reporting/scripts/framework_include.js'></script>
    <script type="text/javascript">
        // otaism2a: 28,1,2014 to reduce multiple BOI.js while loading
        var BOI = parent.BOI;

        $(document).ready(function () {
            $('#submit').live('click', function () {
                request();
            });
            if (parent.current_node) {
                app_id = parent.app_id;
                report_id = parent.current_node.data.key;
            }
            //preload var views options
            BOI.BOIData.list.getVariableViews(app_id, function (data) {
                $(data).find('VIEW').each(function () {
                    var option = "<option value='" + $(this).find('VIEW_ID').text() + "'>" + $(this).find('VIEW_NAME').text() + "</option>";
                    $('select#ZDISPLAY').append(option);
                });
            });
            //preload seltypes
            BOI.BOIData.lookup.getSelectionTypes(function (data) {
                $(data).find('ITEM').each(function () {
                    var text = $(this).find('NAME').text();
                    var value = $(this).find('VALUE').text();
                    $('#SEL_TYPE').append('<option value="' + value + '">' + text + '</option>');
                });
            });
            //preload F4modes
            BOI.BOIData.lookup.getF4Modes(function (data) {
                $(data).find('ITEM').each(function () {
                    var text = $(this).find('NAME').text();
                    var value = $(this).find('VALUE').text();
                    $('#F4_MODE').append('<option value="' + value + '">' + text + '</option>');
                });
            });
            //bind events for input elements
            $('form input[type="text"], form input[type="hidden"], form select').live('change', function () {
                if ($(this).val())
                    variables[$('#ZVARNAME').val()][$(this).attr('id')] = $(this).val();
                else
                    variables[$('#ZVARNAME').val()][$(this).attr('id')] = "";
            });
            //bind selection change event
            $('#ZVARNAME').live('change', function () {
                select();
            });
            //bind checkbox change events
            $('#bi_dep input').live('click', function () {

            $('#VARDEPEND').val('');
              var selections = "";
              $('#bi_dep input').each(function () {
                  if ($(this).is(':checked'))
                      selections += $(this).val() + '&';
              });
              if (selections.indexOf('&') > -1)
                  selections = selections.substr(0, selections.length - 1);
              $('#VARDEPEND').val(selections);
              $('#VARDEPEND').trigger('change');
            });
            if (report_id != undefined)
                load();
        });
        function request() //genereates request XML
        {
            var records, record, class_name, method_name, req, param;
            var done = 0;
            method_name = "UPDATE";

            //UPDATE LAYOUT_INFO
            record = new BOI.BOIData.Record();
            class_name = "ZCL_MYBW_VAR_DEPENDENCY";

            //set dependencies
            var depString = "";
            for (var i in variables) {
                var temp = "|";
                var dependencies = variables[i]['VARDEPEND'].split('|');
                if (dependencies.length > 0 && dependencies[0] != undefined)
                    temp += variables[i]['ZVARNAME'] + '=';
                for (var d in dependencies) {
                    if (dependencies[d] && d < dependencies.length - 1)
                        temp += dependencies[d] + '&';
                    else if (dependencies[d])
                        temp += dependencies[d];
                }
                depString += temp;
            }
            if (depString.indexOf('|') > -1)
                depString = depString.substr(1);

            layout['VAR_DEPENDS'] = depString;

            for (var j in layout) {
                //alert(j + ' = ' + layout[j]);
                if (j) {
                    param = new BOI.BOIData.Parameter();
                    param.name = j;
                    param.values = [layout[j]];

                record.parameters.push(param);
                }
            }
            req = BOI.BOIData.request.prepare(class_name, method_name, [record]);
            BOI.BOIData.request.perform(req, 'text', 'POST', function (data) {

                //update var info
                records = [];
                class_name = "ZCL_MYBW_REP_VAR_CAT";

                for (var i in variables) {
                    if (i) {
                        record = new BOI.BOIData.Record();
                        for (var j in variables[i]) {
                            if (j) {
                                param = new BOI.BOIData.Parameter();
                                param.name = j;
                                param.values = variables[i][j] == null ? [""] : [variables[i][j]];
                                record.parameters.push(param);
                            }
                        }
                        records.push(record);
                  }
                }
                req = BOI.BOIData.request.prepare(class_name, method_name, records);

                BOI.BOIData.request.perform(req, 'text', 'POST', function (data) {
                    parent.reloadAssignment();
                    //alert($(data).find('MESSAGE').text());
                    BOI.BOILib.Util.log($(data).find('MESSAGE').text(),true);
                });
            });
        }

        var variables = {};
        var layout = {};
        var app_id, report_id;
        //initialization and rendering of report variables layout
        function load() {
            //load report
            BOI.BOIData.read.getNode(app_id, report_id, function (data) {
                var varcount = 0;
                data = $(data);

                var form = $('form');
                //set layout values
                data.find('LAYOUT_INFO').children().each(function () {
                    var tag = $(this).get(0).tagName;
                    form.find('#' + tag).val($(this).text());

                    layout[tag] = $(this).text();

                });

                //set variables values
                data.find('VARIABLES ZBUS_VAR_META').each(function () {
                    varcount++;
                    var v = {};
                    $(this).children().each(function () {
                        v[this.nodeName] = $(this).text();
                    });
                    variables[$(this).find('ZVARNAME').text()] = v;
                });

                for (var i in variables) //selector
                {
                    if (i) {
                        var option = "<option value='" + i + "'>" + i + "</option>";
                        $('#ZVARNAME').append(option);
                    }
                }
                if (varcount > 0) {
                    checkBoxList($('#VARDEPEND'), variables); //convert to CB List
                    select();
                }
                else {
                    $('form').hide();
                    $('body').append('<p>This report has no variables.</p>');
                }
            });
        }

        function select() {
            var id = $('#ZVARNAME').val();
            var v = variables[id];
            if (v) {
                $('form input[type="text"], form input[type="hidden"], form select').each(function () {
                    $(this).val(v[$(this).attr('id')]);
                });
                var deps = v['VARDEPEND'].split('&');
                $('#bi_dep input').removeAttr('checked');
                $('#bi_dep li').show();
                $('#bi_dep input[value="' + id + '"]').parent().hide();
                for (var i in deps)
                    if (deps[i]) {
                        $('#bi_dep input[value="' + deps[i] + '"]').attr('checked', true);
                    }
                //trigger change
                $('#VARDEPEND').trigger('change');
            }
        }
        function checkBoxList(input, data, selection) {
            var container = $('<div id="bi_dep"></div>');
            container.insertAfter(input);
            container.append('<ul></ul>');
            for (var i in data) {
                if (i) {
                    var cb = "<li><input type='checkbox' value='" + i + "' />" + i + '</li>';
                    container.find('ul').append(cb);
                }
            }
        }
    </script>
</head>
<body>
    <div class="tab-frame bi_admin_report_edit_variables">
        <form method="post">
            <h2>BI Apps - Edit Report Variables</h2>
            <ul>
                <li>
                    <label for="ZVARNAME">Select Variable:</label><select id="ZVARNAME" name="ZVARNAME"></select></li>
                <li>
                    <hr />
                </li>
                <li>
                    <label for="ZVAR_DESCRIPTION">Description:</label><input id="ZVAR_DESCRIPTION" name="ZVAR_DESCRIPTION" type="text" /></li>
                <li>
                    <label for="DEF_VAL">Default Value:</label><input id="DEF_VAL" name="DEF_VAL" type="text" /></li>
                <li style="display: none;">
                    <label for="VAR_GRP">Layout Group ID:</label><input id="VAR_GRP" name="VAR_GRP" type="text" /></li>
                <li style="display: none;">
                    <label for="VAR_LOC">Layout Location ID:</label><input id="VAR_LOC" name="VAR_LOC" type="text" /></li>
                <li>
                    <label for="ZDISPLAY">Display Template:</label><select id="ZDISPLAY" name="ZDISPLAY"></select></li>
                <li>
                    <label for="SEL_TYPE">Selection Type:</label><select id="SEL_TYPE" name="SEL_TYPE"></select></li>
                <li>
                    <label for="F4_MODE">F4 Lookup Mode:</label><select id="F4_MODE" name="F4_MODE"></select></li>
                <li>
                    <label for="F4_QUERY">F4 Query (for BW_Report mode):</label><input id="F4_QUERY" name="F4_QUERY" type="text" /></li>
                <li>

     <label for="VARDEP_QUERY">Dependency Query:</label><input type="text" id="VARDEP_QUERY" name="VARDEP_QUERY" /></li>
                <li>
                    <label for="VARDEPEND">Dependencies:</label><div class="input-hidden">
                        <input type="hidden" id="VARDEPEND" name="VARDEPEND" />
                    </div>
                </li>

                <li>
                    <a id="submit" class="button-content-YW" href="#"><span>Save</span></a></li>

            </ul>
        </form>
    </div>
</body>
</html>